/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */
 
#ifndef OBD_PID_H
#define OBD_PID_H
 
/*To use the PID length lookup table.*/
extern const char pid_length[];
extern const char *pid_name[];

#define MAX_PID 0x52

/*The following statement returns the length of PID A fromthe PID lookup table.*/
#define PID_LENGTH(A) ( (MAX_PID >= A)?pid_length[A]:0 )
 
/*OBD-II PIDs*/ 
 
//*Mode 1 PIDs*/
#define PID_SUPPORTED	0x00 	//Which PIDs are supported. Bit encoded. PIDs 0x01-0x20
#define STATUS_MILDTC	0x01  	//Monitor status for MIL and DTCs. Bit encoded.
#define FREEZE_DTC		0x02	//Diagnostic Trouble codes
#define STATUS_FUELSYS	0x03	//Current fuel system status. Bit encoded.
#define	ENGINE_LOAD		0x04	//Engine load value.
#define	ENGINE_TEMP		0x05	//The engine coolant temperature.

#define	SHORT_TERM_FUEL_TRIM_B1 0x06 //Short term fuel % trim, Bank 1.
#define LONG_TERM_FUEL_TRIM_B1	0x07 //Long term fuel % trim ,Bank 1.
#define	SHORT_TERM_FUEL_TRIM_B2 0x08 //Short term fuel % trim, Bank 2.
#define LONG_TERM_FUEL_TRIM_B2	0x09 //Long term fuel % trim ,Bank 2.
 
#define PRESSURE_FUEL		0x0A 	//Fuel pressure. 
#define	PRESSURE_MANIFOLD	0x0B	//Intake manifold pressure.
 
#define	ENGINE_RPM		0x0C	//Engine RPMs.
#define VEHICLE_SPEED	0x0D	//Speed of the vehicle.
#define TIMING_ADVANCE	0x0E	//Engine timing, relative to cylinder 1.
#define	INTAKE_TEMP		0x0F	//Intake air temperature.
#define	MAF_AIRFLOW		0x10	//MAF Air flow rate.
#define	THROTTLE_POS	0x11	//Throttle position.
#define	STATUS_CSA		0x12	//Secondary commanded air status (bit encoded.)
 
#define	OXY_PRESENT		0x13	//If oxygen sensors are present.
#define	OXY_B1_S1		0x14	//Bank1, Sensor 1
#define	OXY_B1_S2		0x15	//Bank1, Sensor 2
#define OXY_B1_S3		0x16	//Bank1, Sensor 3
#define OXY_B1_S4		0x17	//Bank1, Sensor 4
#define OXY_B2_S1		0x18	//Bank2, Sensor 1
#define OXY_B2_S2		0x19	//Bank2, Sensor 2
#define OXY_B2_S3		0x1A	//Bank3, Sensor 3
#define OXY_B2_S4		0x1B	//Bank3, Sensor 4
 
#define OBD_STANDARD	0x1C	//Which OBD standard this vehicle supports.
#define	OXY_PRESENT2	0x1D	//Similar to PID 0x13, but bit order changed. 
 
#define STATUS_AUX		0x1E	//Auxilary input status.
#define ENGINE_RUNTIME	0x1F	//Run time since engine start.
 
#define PID_SUPPORTED2	0x20	//Which PIDs are supported. Bit encoded. PIDs 0x21-0x40
#define DISTANCE_WMIL	0x21	//Distance travelled with MIL on.
#define PRESS_FUELRAIL	0x22	//Fuel rail pressure relative to manifold vacuum.
#define PRESS_FUELDIESEL 0x23	//Fuel rail pressure (Diesel)
 
#define OS2S1_WR_LAMBDA	0x24	//Equivalence ratio voltage
#define OS2S2_WR_LAMBDA 0x25	//Equivalence ratio voltage 
#define OS2S3_WR_LAMBDA 0x26 	//Equivalence ratio voltage
#define OS2S4_WR_LAMBDA	0x27	//Equivalence ratio voltage
#define OS2S5_WR_LAMBDA	0x28	//Equivalence ratio voltage
#define OS2S6_WR_LAMBDA	0x29	//Equivalence ratio voltage 
#define OS2S7_WR_LAMBDA	0x2A	//Equivalence ratio voltage
#define OS2S8_WR_LAMBDA	0x2B	//Equivalence ratio voltage
 
#define COMMANDED_EGR	0x2C	//Commanded EGR.
#define ERROR_EGR	0x2D	//EGR Error.
#define FUEL_LEVEL	0x2F //Fuel level input. %

#define PID_SUPPORTED3	0x40 	//PIDs supported 21-60.
#define FUEL_TYPE		0x51	//Type of fuel. 01-Gasoline, 02-Methanol, 03-Ethanol, 04-Diesel (10+Gasoline, etc. is electric hybrid. I.e. 11 is gasoline hybrid.)
#define FUEL_ETH_PER	0x52	//Ethanol fuel %. 
 
/*Mode 2 PIDs*/

#define FREEZE_FRAME_DTC	0x02
 
/*Mode 3 PIDs
No PIDs required. Mode 3 returns BCD encoded DTC values.
Returns 6 byte packets.
Each DTC takes 2 bytes. The number of packets is the number of
codes divided by 3. 
 
Each DTC is composed of 5 characters
First character - 2 bit encoded Byte0[7..6] (P,C,B,U)
Second character - 2 bit encoded Byte0[5..4] (0,1,2,3)
Third character - 4 bit encoded Byte0[3..0] (1,2,3,4,5,6,7,8,9)
Fourth character - 4 bit encoded Byte1[7..4] (1,2,3,4,5,6,7,8,9)
Fifth character - 4 bit encoded Byte1[3..0] (1,2,3,4,5,6,7,8,9);
*/

/* [A] is expected to be in memory as [Byte1][Byte0] . BUF is a buffer that has atleast 6 characters (5 character + null character.)*/
#define DTC_CODE(BUF,A) sprintf(BUF, "%c%d%d%d%d", 	( (((A>>6)&0x3)==0)?'P':( (((A>>6)&0x3)==1)?'C':( (((A>>6)&0x3)==2)?'B':( (((A>>6)&0x3)==3)?'U':'?') ) ) ), \
													((A>>4)&0x3), \
													((A>>0)&0xF), \
													((A>>12)&0xF),\
													((A>>8) &0xF) )

/*Mode 5 PIDs
Oxygen sensor data. */
 
/*Mode 9 PIDs*/
#define VIN	0x02 //Vehicle VIN number. Returns 5 lines.
 
/*Defines for conversion.*/
#define CONV_ENGINE_LOAD(A)	(A*100.0/255.0) //Converted engine load value (%)
#define CONV_ENGINE_TEMP(A)	(A-40.0)		//Converted engine coolant temp. (deg C)
#define CONV_SHORT_TERM_FUEL_TRIM_B1(A)	(A-128.0)*(100.0/128.0) 
#define CONV_LONG_TERM_FUEL_TRIM_B1(A)	(A-128.0)*(100.0/128.0) 
#define CONV_SHORT_TERM_FUEL_TRIM_B2(A)	(A-128.0)*(100.0/128.0) 
#define CONV_LONG_TERM_FUEL_TRIM_B2(A)	(A-128.0)*(100.0/128.0) 
#define CONV_PRESSURE_FUEL(A)	(A*3.0) //Fuel pressure (kPa -- gauged.)
#define	CONV_PRESSURE_MANIFOLD(A)	(A) //Intake manifold pressure (kPa -- absolute.)
#define CONV_ENGINE_RPM(A)	(A/4.0) //Engine RPMs. 2 Bytes of data. A = B0<<8 + B1
#define CONV_VEHICLE_SPEED(A)	(A)	//Vehicle speed (absolute.)
#define CONV_TIMING_ADVANCE(A) 	(A/2.0) - 64.0
#define CONV_INTAKE_TEMP(A)	(A-40.0) //Intake air temperature (deg C)
#define CONV_MAF_AIRFLOW(A)	(A/100.0) //MAF Air flow rate. Gallons/second.
#define CONV_THROTTLE_POS(A)	(A*(100.0/255.0)) //Throttle position (%)
#define CONV_ENGINE_RUNTIME(A)	(A) //Two bytes of data. B0<<8 + B1. Seconds.
#define CONV_DISTANCE_WMIL(A)	(A) //Distance travelled with MIL (km)
#define CONV_PRESS_FUELRAIL(A)	(A*0.079) //Fuel rail pressure in kPa
#define CONV_PRESS_FUELDIESEL(A)	(A*10) //Diesel fuel rail pressure. Two bytes of data. kPa (gauge)
#define CONV_FUEL_LEVEL(A)	(A*(100.0/255.0)) //Fuel level input. %
#define CONV_FUEL_ETH_PER(A) 	(A*(100.0/255.0)) //Ethanol fuel %

/*PID Lengths*/
#define LEN_M0100 4
#define LEN_M0101 4
#define LEN_M0102 8
#define LEN_M0103 2
#define LEN_M0104 1
#define LEN_M0105 1
#define LEN_M0106 1
#define LEN_M0107 1
#define LEN_M0108 1
#define LEN_M0109 1
#define LEN_M010A 1
#define LEN_M010B 1
#define LEN_M010C 2
#define LEN_M010D 1
#define LEN_M010E 1
#define LEN_M010F 1
#define LEN_M0110 2
#define LEN_M0111 1
#define LEN_M0112 1
#define LEN_M0113 1
#define LEN_M0114 2
#define LEN_M0115 2
#define LEN_M0116 2
#define LEN_M0117 2
#define LEN_M0118 2
#define LEN_M0119 2
#define LEN_M011A 2
#define LEN_M011B 2
#define LEN_M011C 1
#define LEN_M011D 1
#define LEN_M011E 1
#define LEN_M011F 2
#define LEN_M0120 4
#define LEN_M0121 2
#define LEN_M0122 2
#define LEN_M0123 2
#define LEN_M0124 4
#define LEN_M0125 4
#define LEN_M0126 4
#define LEN_M0127 4
#define LEN_M0128 4
#define LEN_M0129 4
#define LEN_M012A 4
#define LEN_M012B 4
#define LEN_M012C 1
#define LEN_M012D 1
#define LEN_M012E 1
#define LEN_M012F 1
#define LEN_M0130 1
#define LEN_M0131 2
#define LEN_M0132 2
#define LEN_M0133 1
#define LEN_M0134 4
#define LEN_M0135 4
#define LEN_M0136 4
#define LEN_M0137 4
#define LEN_M0138 4
#define LEN_M0139 4
#define LEN_M013A 4
#define LEN_M013B 4
#define LEN_M013C 2
#define LEN_M013D 2
#define LEN_M013E 2
#define LEN_M013F 2
#define LEN_M0140 4
#define LEN_M0141 4
#define LEN_M0142 2
#define LEN_M0143 2
#define LEN_M0144 2
#define LEN_M0145 1
#define LEN_M0146 1
#define LEN_M0147 1
#define LEN_M0148 1
#define LEN_M0149 1
#define LEN_M014A 1
#define LEN_M014B 1
#define LEN_M014C 1
#define LEN_M014D 2
#define LEN_M014E 2
#define LEN_M014F 0
#define LEN_M0150 0
#define LEN_M0151 1		
#define LEN_M0152 1														

#endif 
